﻿namespace GoodAdmin.System;

/// <inheritdoc cref="IExtraUserService"/>
public class ExtraUserService : IExtraUserService, ITransient
{
    private readonly IBaseService _baseService;
    private readonly ISysUserService _sysUserService;

    public ExtraUserService(IBaseService baseService, ISysUserService sysUserService)
    {
        _baseService = baseService;
        _sysUserService = sysUserService;
    }
    /// <inheritdoc/>
    public async Task AddUser(UserAddInput userInput)
    {
        await _baseService.CheckDataScope(userInput.OrgId);
        await _sysUserService.AddUser(userInput);
    }


    /// <inheritdoc/>
    public async Task UpdateUser(UserUpdateInput userInput)
    {
        await CheckDataScope(userInput.Id);
        await _sysUserService.UpdateUser(userInput);
    }

    /// <inheritdoc/>
    public async Task DeleteUser(List<BaseIdInput> Input)
    {
        var idList= Input.Select(x => x.Id).ToList();
        var userList= await _sysUserService.GetUserList(idList);
        await _baseService.CheckDataScope(userList.Select(u => u.OrgId).ToList());
        await _sysUserService.DeleteUser(Input);
    }

    /// <inheritdoc />
    public async Task<bool> ChangeStatus(BaseIdInput input)
    {
        await CheckDataScope(input.Id);
        var user = await _sysUserService.GetUserById(input.Id);
        //如果当前用户状态为有效，那么就禁用用户
        if (user.Status == CommonConst.STATUS_DISABLE)
        {
            return await _sysUserService.EnableUser(input);
        }
        else
        {
            return await _sysUserService.DisableUser(input);
        }       
    }

    /// <inheritdoc />
    public async Task GrantUserRole(UserRoleInput input)
    {
        await _sysUserService.GrantUserRole(input);
    }
    /// <inheritdoc />
    public async Task<SqlSugarPagedList<SysUser>> Page(UserPageInput input)
    {
        if (!UserManager.SuperAdmin)
        {
            var dataScope = await _baseService.GetUserDataScopeByRequest();
            var exp = Expressionable.Create<SysUser>();
            exp.And(u => u.Account != RoleConst.SUPERADMIN);
            //用户ID等于自己
            if (dataScope.Count == 0) exp.And(u => u.Id == UserManager.UserId);
            //用户机构在数据范围内
            else exp.And(u => dataScope.Contains(u.OrgId));
            input.Expression = exp;
        }
        return await _sysUserService.GetUserPage(input);
    }
    /// <inheritdoc />
    public async Task ResetPassword(BaseIdInput input)
    {
        await CheckDataScope(input.Id,true);
        await _sysUserService.ResetPassword(input);
    }

    /// <inheritdoc />
    public async Task ChangePassword(ChangePwdInput input)
    {
        //如果不是超级管理员，且不是自己
        if (!UserManager.SuperAdmin && input.Id != UserManager.UserId)
        {
            throw Oops.Bah(UserErrorCode.U012);
        }
        var user = await _sysUserService.GetUserById(input.Id);
        if (CryptogramUtil.Sm2Decrypt(user.Password) != input.PasswordOld)
            throw Oops.Bah(UserErrorCode.U013);
        await _sysUserService.ChangePassword(input);
    }
    /// <summary>
    /// 检查数据范围
    /// </summary>
    /// <param name="UserId"></param>
    /// <param name="CheckSelf">检查是否为自己</param>
    /// <returns></returns>
    private async Task CheckDataScope(long UserId, bool CheckSelf = false)
    {

        var user = await _sysUserService.GetUserById(UserId);
        UserId = CheckSelf ? UserId : 0;
        await _baseService.CheckDataScope(user.OrgId, UserId);
    }
}